cell_dependencies$243063ab-0556-4fcb-950a-f7533a2f88faprecedence_heuristic cell_id$243063ab-0556-4fcb-950a-f7533a2f88fadownstream_cells_mapN$f3bf1b25-cb37-4594-b92b-fcd3f44429e9$bb9d1888-9214-43d2-b002-b31ded8715a9upstream_cells_map$d9ca9f51-d5dd-4321-bae8-fec16a53d250precedence_heuristic cell_id$d9ca9f51-d5dd-4321-bae8-fec16a53d250downstream_cells_mapPoint$c5a0d444-770d-4dc8-8267-963d9f2be545$624c3dc0-2186-4185-9fba-87e522d4a163Bounds$bb9d1888-9214-43d2-b002-b31ded8715a9upstream_cells_mapInt$39556336-9d1c-417a-b3d7-4e424710902dprecedence_heuristic cell_id$39556336-9d1c-417a-b3d7-4e424710902ddownstream_cells_mapsimulated_annealing_tsp$624c3dc0-2186-4185-9fba-87e522d4a163upstream_cells_map>islessrandpermcopylength rand() current_tour = copy(next_tour) current_distance = next_distance # If the new tour is the best found so far, update the best tour and distance if current_distance < best_distance best_tour = copy(current_tour) best_distance = current_distance end end temperature *= cooling_rate # Cool down the temperature end return best_tour end;metadatashow_logsèdisabled®skip_as_script«code_folded$7c0f26a0-a478-4887-91c8-e5a747b97c19cell_id$7c0f26a0-a478-4887-91c8-e5a747b97c19codefunction total_distance(points, tour) total_distance = 0.0 n = length(points) for i in 1:n-1 p1 = points[tour[i]] p2 = points[tour[i+1]] total_distance += evaluate(Euclidean(), [p1.x, p1.y], [p2.x, p2.y]) end p_last = points[tour[end]] p_first = points[tour[1]] # Return to the starting point total_distance += evaluate(Euclidean(), [p_last.x, p_last.y], [p_first.x, p_first.y]) return total_distance end;metadatashow_logsèdisabled®skip_as_script«code_folded$fdfdbb5c-2204-434c-8249-bc9ad3e72531cell_id$fdfdbb5c-2204-434c-8249-bc9ad3e72531codexbegin get_points_xs(points) = [point.x for point in points] get_points_ys(points) = [point.y for point in points] end;metadatashow_logsèdisabled®skip_as_script«code_folded$069a3cdc-6f8e-4470-a6a8-8312eb6b5df5cell_id$069a3cdc-6f8e-4470-a6a8-8312eb6b5df5codeyour_solution = [1,2,3,4,5]metadatashow_logsèdisabled®skip_as_script«code_folded$739b8137-f6b6-4516-b28d-acdb79828be6cell_id$739b8137-f6b6-4516-b28d-acdb79828be6codeyfunction create_points(b, N) points = [] for i in 1:N push!(points, random_point(b, points)) end return points end;metadatashow_logsèdisabled®skip_as_script«code_folded$f3bf1b25-cb37-4594-b92b-fcd3f44429e9cell_id$f3bf1b25-cb37-4594-b92b-fcd3f44429e9codeif length(your_solution) == N @htl("""
""") else @htl("""
Make sure  
length(your_solution) == length(N)
""") endmetadatashow_logsèdisabled®skip_as_script«code_folded$5bf31410-c914-11ee-33f0-c5c134d6b4d8cell_id$5bf31410-c914-11ee-33f0-c5c134d6b4d8codeRbegin using Distances using HypertextLiteral using Random using Statistics endmetadatashow_logsèdisabled®skip_as_script«code_folded$d25e7bed-3aae-43a1-aa5c-ef44cca55242cell_id$d25e7bed-3aae-43a1-aa5c-ef44cca55242codebegin truck_url = "https://raw.githubusercontent.com/mthelm85/travelling-salesman-game/main/assets/car2.png" truck2_url = "https://raw.githubusercontent.com/mthelm85/travelling-salesman-game/main/assets/car5.png" house_url = "https://raw.githubusercontent.com/mthelm85/travelling-salesman-game/main/assets/house1.png" map_url = "https://raw.githubusercontent.com/mthelm85/travelling-salesman-game/main/assets/grass.png" end;metadatashow_logsèdisabled®skip_as_script«code_folded$624c3dc0-2186-4185-9fba-87e522d4a163cell_id$624c3dc0-2186-4185-9fba-87e522d4a163codebegin points_xs = get_points_xs(points) points_ys = get_points_ys(points) distances = pairwise(Euclidean(), [points_xs points_ys], dims=1) max_iter = 1000 initial_temperature = mean(distances) + 3 * std(distances) cooling_rate = 0.99 solution_player = vcat(your_solution, your_solution[1]) player_xs = get_points_xs(points[solution_player]) player_ys = get_points_ys(points[solution_player]) solution_computer = simulated_annealing_tsp(points, max_iter, initial_temperature, cooling_rate) push!(solution_computer, solution_computer[1]) # return to start computer_xs = get_points_xs(points[solution_computer]) computer_ys = get_points_ys(points[solution_computer]) player_distance = total_distance([Point(z[1], z[2]) for z in zip(player_xs, player_ys)], 1:size(player_xs, 1)) computer_distance = total_distance([Point(z[1], z[2]) for z in zip(computer_xs, computer_ys)], 1:size(computer_xs, 1)) end;metadatashow_logsèdisabled®skip_as_script«code_folded$c5a0d444-770d-4dc8-8267-963d9f2be545cell_id$c5a0d444-770d-4dc8-8267-963d9f2be545codefunction random_point(bounds, points) x = rand(bounds.x:bounds.x + bounds.width - 1) y = rand(bounds.y:bounds.y + bounds.height - 1) # make sure points aren't too close together threshold = 20 for point in points if abs(point.x - x) < threshold || abs(point.y - y) < threshold return random_point(bounds, points) end end return Point(x, y) end;metadatashow_logsèdisabled®skip_as_script«code_folded$2ee01ff1-a85f-4ff9-8d25-6af053b2d8aacell_id$2ee01ff1-a85f-4ff9-8d25-6af053b2d8aacodemd""" ## The Traveling Salesman Game The Traveling Salesman Problem (TSP) is a classic optimization problem in the fields of computer science and operations research. It involves finding the shortest possible route that visits a set of given cities once, and then returns to the original city. The challenge is to determine the optimal ordering of the cities to minimize the total distance traveled by the salesman. In this notebook, you get a chance to try to solve the problem while competing against the computer, which solves it using an algorithm known as *Simulated Annealing*, an optimization algorithm inspired by the annealing process in metallurgy. It is used to solve combinatorial optimization problems like the TSP. #### How to Play In the cell below, set `N` to the number of cities (or houses in our case) that you would like to start with. These will be generated randomly. Then, set `your_solution` to the path that you believe is the shortest. There is no need to add the starting point to the end of the vector; there is code below that handles that for you. Once you have entered your solution, push the `Go!` button and see if you beat the computer! To play another round, or to generate new points, re-run the cell that defines `N`. """metadatashow_logsèdisabled®skip_as_script«code_folded$bb9d1888-9214-43d2-b002-b31ded8715a9cell_id$bb9d1888-9214-43d2-b002-b31ded8715a9codeHbegin b = Bounds(100, 100, 450, 280) points = create_points(b, N) end;metadatashow_logsèdisabled®skip_as_script«code_folded«notebook_id$ccea4c1c-75f4-11f1-a06e-950636a5720bbondscell_results$243063ab-0556-4fcb-950a-f7533a2f88faqueued¤logsrunning¦outputbody5persist_js_state¤mimetext/plainlast_run_timestampAڑ~
persist_js_state¤mimetext/htmllast_run_timestampAڑ has_pluto_hook_features¬rootassigneecell_id$f3bf1b25-cb37-4594-b92b-fcd3f44429e9depends_on_disabled_cells§runtimer%˵published_object_keysdepends_on_skipped_cells§errored$5bf31410-c914-11ee-33f0-c5c134d6b4d8queued¤logsrunning¦outputbodypersist_js_state¤mimetext/plainlast_run_timestampAڑ}#has_pluto_hook_features¬rootassigneecell_id$5bf31410-c914-11ee-33f0-c5c134d6b4d8depends_on_disabled_cells§runtimespublished_object_keysdepends_on_skipped_cells§errored$d25e7bed-3aae-43a1-aa5c-ef44cca55242queued¤logsrunning¦outputbodypersist_js_state¤mimetext/plainlast_run_timestampAڑ~#has_pluto_hook_features¬rootassigneecell_id$d25e7bed-3aae-43a1-aa5c-ef44cca55242depends_on_disabled_cells§runtimesKpublished_object_keysdepends_on_skipped_cells§errored$624c3dc0-2186-4185-9fba-87e522d4a163queued¤logsrunning¦outputbodypersist_js_state¤mimetext/plainlast_run_timestampAڑ1has_pluto_hook_features¬rootassigneecell_id$624c3dc0-2186-4185-9fba-87e522d4a163depends_on_disabled_cells§runtimehǏpublished_object_keysdepends_on_skipped_cells§errored$c5a0d444-770d-4dc8-8267-963d9f2be545queued¤logsrunning¦outputbodypersist_js_state¤mimetext/plainlast_run_timestampAڑ

The Traveling Salesman Game

The Traveling Salesman Problem (TSP) is a classic optimization problem in the fields of computer science and operations research. It involves finding the shortest possible route that visits a set of given cities once, and then returns to the original city. The challenge is to determine the optimal ordering of the cities to minimize the total distance traveled by the salesman.

In this notebook, you get a chance to try to solve the problem while competing against the computer, which solves it using an algorithm known as Simulated Annealing, an optimization algorithm inspired by the annealing process in metallurgy. It is used to solve combinatorial optimization problems like the TSP.

How to Play

In the cell below, set N to the number of cities (or houses in our case) that you would like to start with. These will be generated randomly. Then, set your_solution to the path that you believe is the shortest. There is no need to add the starting point to the end of the vector; there is code below that handles that for you. Once you have entered your solution, push the Go! button and see if you beat the computer!

To play another round, or to generate new points, re-run the cell that defines N.

persist_js_state¤mimetext/htmllast_run_timestampAڑT7׷has_pluto_hook_features¬rootassigneecell_id$2ee01ff1-a85f-4ff9-8d25-6af053b2d8aadepends_on_disabled_cells§runtimerpublished_object_keysdepends_on_skipped_cells§errored$bb9d1888-9214-43d2-b002-b31ded8715a9queued¤logsrunning¦outputbodypersist_js_state¤mimetext/plainlast_run_timestampAڑhas_pluto_hook_features¬rootassigneecell_id$bb9d1888-9214-43d2-b002-b31ded8715a9depends_on_disabled_cells§runtime\published_object_keysdepends_on_skipped_cells§errored©shortpathTraveling Salesman Game.jllast_save_timeAڑT0FEin_temp_dir¨metadatafrontmatterlicense_url)https://opensource.org/license/unlicense/authornameMatt Helmurlhttps://github.com/mthelm85imageٿhttps://upload.wikimedia.org/wikipedia/commons/thumb/1/10/Travelling_salesman_problem_solved_with_simulated_annealing.gif/500px-Travelling_salesman_problem_solved_with_simulated_annealing.giftitleTraveling Salesman Gametagsoptimizationpuzzletraveling-salesmanlicenseUnlicensedescription_A fun game to learn about the traveling salesman problem and the simulated annealing algorithm.date2024-02-16